package com.lsk.util;

import com.aliyun.oss.OSSClient;
import com.jfinal.upload.UploadFile;

import java.io.File;
import java.io.InputStream;

/**
 * Created by Administrator on 2018/10/12 0012.
 */
public class OssUpload {
    public static void fileUpload(UploadFile uploadFile, String key){
        String endpoint = "http://"+GlobalParameter.OOSUrl;
        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
        String accessKeyId = GlobalParameter.accessKeyId;
        String accessKeySecret = GlobalParameter.accessKeySecret;

        // 创建OSSClient实例。
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        try {
            // 上传网络流。
            ossClient.putObject(GlobalParameter.bucketName, key, uploadFile.getFile());
            // 关闭OSSClient。

            ossClient.shutdown();
        }catch (Exception e){
            ossClient.shutdown();
        }finally {
            FileUtil.delFile(uploadFile.getUploadPath()+uploadFile.getFileName());
            ossClient.shutdown();
        }

    }
}
